import buildingMgr from '@/biz/building/index.js'
import ruleOpt from '@/biz/option/rule.js'

function listByCityId(params) {
  return buildingMgr.getAreacodePropertyList(params).then((res) => {
    return res.list.map((item) => {
      item.key = item.cityCode.toString()
      item.value = item.cityName
      return item
    })
  })
}

function getStreetList(params) {
  return buildingMgr.getStreetList(params).then((res) => {
    return res.list.map((item) => {
      item.key = item.streetCode.toString()
      item.value = item.streetName
      return item
    })
  })
}

// 省市联动 层级(1:国 2:省 3:市 4:区,5:街道)
function getareaCodeList(level = 2, parentCode = '', city = '') {
  let params = {
    page: '1',
    rows: '50',
    level: level,
    parentCode: parentCode
  }
  console.log('zhk ~ file: city.js:32 ~ getareaCodeList ~ params:', params)
  if (city != '') {
    params.cityCode = level == 5 ? city : ''
    return getStreetList(params).then((res) => {
      return res
    })
  } else {
    return listByCityId(params).then((res) => {
      console.log('zhk ~ file: city.js:40 ~ returnlistByCityId ~ res:', res)
      return res
    })
  }
}

function rulesCity(msg, ok) {
  let validator = (rule, value, callback) => {
    if (ok(value)) {
      return callback(msg)
    }
    callback()
  }
  return {
    validator,
    trigger: 'blur,change'
  }
}

function city(opt) {
  console.log(opt.info, '城市信息')
  let province = {
    key: 'provinceCode',
    col: 6,
    placeholder: '请选择省',
    disabled: opt.isView,
    load: (o) => {
      getareaCodeList().then((res) => {
        o.select.options = res
      })
    },
    select: {
      options: []
    },
    change: (data) => {
      console.log(data, 'provinceCode')
      data.info.provinceName = data.editValue ? data.editValue.label : ''
      street.select.options = []
      data.info.streetCode = ''
      data.info.streetName = ''
      getareaCodeList(3, data.value).then((res) => {
        city.select.options = res
      })
    }
  }
  let city = {
    key: 'cityCode',
    col: 6,
    placeholder: '请选择市',
    disabled: opt.isView,
    load: (o) => {
      if (opt.info.provinceCode) {
        getareaCodeList(3, opt.info.provinceCode).then((res) => {
          o.select.options = res
          if (opt.info.areaCode && opt.info.cityCode) {
            getareaCodeList(5, opt.info.areaCode, opt.info.cityCode).then((res) => {
              street.select.options = res
            })
          }
        })
      }
    },
    select: {
      options: []
    },
    change: (data) => {
      console.log(data, 'cityCode')
      data.info.cityName = data.editValue ? data.editValue.label : ''
      street.select.options = []
      data.info.streetCode = ''
      data.info.streetName = ''
      getareaCodeList(4, data.value).then((res) => {
        area.select.options = res
      })
    }
  }
  let area = {
    key: 'areaCode',
    col: 6,
    placeholder: '请选择区',
    disabled: opt.isView,
    load: (o) => {
      if (opt.info.cityCode) {
        getareaCodeList(4, opt.info.cityCode).then((res) => {
          o.select.options = res
        })
      }
    },
    select: {
      options: []
    },
    change: (data) => {
      console.log(data, 'areaCode')
      data.info.areaName = data.editValue ? data.editValue.label : ''
      getareaCodeList(5, data.value, data.info.cityCode).then((res) => {
        street.select.options = res
      })
    }
  }
  let street = {
    key: 'streetCode',
    col: 6,
    placeholder: '请选择街道',
    disabled: opt.isView,
    load: (o) => {
      if (opt.info.areaCode) {
        getareaCodeList(5, opt.info.areaCode, opt.info.cityCode).then((res) => {
          o.select.options = res
        })
      }
    },
    select: {
      options: [],
      clearable: true
    },
    change: (data) => {
      console.log(data, 'streetCode')
      data.info.streetName = data.editValue ? data.editValue.label : ''
    }
  }

  function group() {
    return [province, city, area, street]
  }
  let option = {
    title: '所属城市',
    key: 'provinceCode',
    labelWidth: opt.labelWidth ? opt.labelWidth : 150,
    col: opt.col || 24,
    group: group(),
    rules: [
      ruleOpt.required('所属城市不能为空'),
      rulesCity('请选择所属城市', (value) => {
        if (opt.checkLevel == 'level1') {
          let flag = true
          flag = opt.info.provinceCode == null || opt.info.provinceCode == ''
          return flag
        } else {
          let flag = true
          flag =
            opt.info.provinceCode == null ||
            opt.info.provinceCode == '' ||
            opt.info.cityCode == null ||
            opt.info.cityCode == '' ||
            opt.info.areaCode == null ||
            opt.info.areaCode == ''
          // || opt.info.streetCode == null || opt.info.streetCode == ''
          return flag
        }
      })
    ]
  }

  if (opt.visible !== '') {
    option.visible = opt.visible
  }

  return option
}
export default {
  city,
  getareaCodeList
}
